package agent.componet.anwser;


import agent.componet.anwser.base.AiResponseHandlerChain;
import agent.componet.anwser.base.ResponseContext;
import agent.componet.message.ChatMessageManager;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.output.FinishReason;
import dev.langchain4j.model.output.Response;

// 使用示例
public class AiChainExample {
    private static final int COUNT = 5;
    private final OpenAiChatModel openAiChatModel;
    private final ChatMessageManager chatUserMessageManagger;

    public AiChainExample(OpenAiChatModel model, ChatMessageManager manager) {
        this.openAiChatModel = model;
        this.chatUserMessageManagger = manager;
    }

    public void processUserRequest(Integer userId) {
        // 1. 获取AI响应
        Response<AiMessage> generate = openAiChatModel.generate(
                chatUserMessageManagger.getRecentMessages(userId, COUNT)
        );

        // 2. 创建上下文
        ResponseContext context = new ResponseContext(userId, generate);

        // 3. 构建处理链
        AiResponsePipeline pipeline = new AiResponsePipeline()
                .addLast(this::handleStopReason)
                .addLast(this::handleToolExecution)
                .addLast(this::handleContentFilter)
                .addLast(this::handleLengthLimit)
                .addLast(this::handleOtherReasons);

        // 4. 执行链式处理
        pipeline.process(context);

        // 5. 处理最终结果
        System.out.println("最终处理结果: " + context.getResult());
    }

    // 处理STOP状态
    private boolean handleStopReason(ResponseContext context, AiResponseHandlerChain chain) {
        if (FinishReason.STOP.equals(context.getFinishReason())) {
            context.setResult("正常结束: " + context.getAiResponse().content().text());
            return false; // 终止链式调用
        }
        chain.proceed(context); // 继续下一个处理器
        return true;
    }

    // 处理工具调用
    private boolean handleToolExecution(ResponseContext context, AiResponseHandlerChain chain) {
        if (FinishReason.TOOL_EXECUTION.equals(context.getFinishReason())) {
            // 执行工具调用逻辑
            String toolResult = executeTool(context.getAiResponse());
            context.setResult("工具执行结果: " + toolResult);
            return false;
        }
        chain.proceed(context);
        return true;
    }

    // 处理内容过滤
    private boolean handleContentFilter(ResponseContext context, AiResponseHandlerChain chain) {
        if (FinishReason.CONTENT_FILTER.equals(context.getFinishReason())) {
            context.setResult("内容被过滤: 不符合安全规范");
            return false;
        }
        chain.proceed(context);
        return true;
    }

    // 处理长度限制
    private boolean handleLengthLimit(ResponseContext context, AiResponseHandlerChain chain) {
        if (FinishReason.LENGTH.equals(context.getFinishReason())) {
            context.setResult("内容过长: 需要继续生成");
            // 可以在这里调用模型继续生成
            return false;
        }
        chain.proceed(context);
        return true;
    }

    // 处理其他情况
    private boolean handleOtherReasons(ResponseContext context, AiResponseHandlerChain chain) {
        context.setResult("其他情况: " + context.getFinishReason().name());
        return false;
    }

    // 工具执行逻辑
    private String executeTool(Response<AiMessage> response) {
        // 实际工具调用实现
        return "工具调用成功";
    }





}
